package vip.george.p0028;

public class P28 {


    /**
     * 方法1：
     * 暴力破解法，依次遍历
     * @param haystack
     * @param needle
     * @return
     */
    public int strStr(String haystack, String needle) {
        int result = -1;
        int hLength = haystack.length();
        int nLength = needle.length();
        //遍历haystack,最多只需要遍历到index = hLength - nLength
        for (int i = 0; i <= hLength - nLength; i++) {
            boolean isMatch = true;
            //遍历needle的全部字符
            for (int j=0; j < nLength; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    isMatch = false;
                    break;
                }
            }
            if (isMatch) {
                result = i;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        P28 p = new P28();
        System.out.println(p.strStr("sadbutsad", "sad"));
        System.out.println(p.strStr("leetcode", "leeto"));
    }

}
